import { createRouter,createWebHashHistory} from "vue-router";
import register from '../register/Register.vue'
import helloworld from '../../components/HelloWorld.vue'
import login from '../login/login.vue'
import roomlist from '../room/roomlist.vue'
import gameroom from '../room/gameroom.vue'


const routerHistory = createWebHashHistory()
const routes =[
    {path: '/',name: 'home',component: helloworld},
    {path: '/register',name: 'register',component: register},
    {path: '/login',name: 'login',component: login},
    {path: '/roomlist',name: 'roomlist',component: roomlist,meta: { requiresAuth: true }},
    {path: '/gameroom',name: 'gameroom',component: gameroom,meta: { requiresAuth: true }},
    
]
const router = createRouter({
    history:routerHistory,
    routes: routes
  })
 export default router

router.beforeEach((to,from,next)=>{
    // const isLogin = localStorage.getItem('accessToken') ; //是否登錄
    const isLogin = sessionStorage.getItem('accessToken') ; //是否登錄
    if(to.meta.requiresAuth){
        if(isLogin){
            next();
        }else{
            next({
                name:'login',
            });
        }
    }else{
        next();
    }
});